package org.boulderfoodrescue.webapp.client.recipients;

import java.util.Comparator;

import org.boulderfoodrescue.webapp.client.donors.DonorRecipientInterface;
import org.boulderfoodrescue.webapp.client.resources.Resources;
import org.boulderfoodrescue.webapp.shared.Location;

import com.google.gwt.cell.client.ClickableTextCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.cellview.client.ColumnSortEvent.ListHandler;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.view.client.ListDataProvider;

public class RecipientTable extends FlowPanel{
  protected CellTable<Location> table;
  protected ListDataProvider<Location> dataProvider;
  public RecipientTable(final DonorRecipientInterface recipientInterface){
    dataProvider = new ListDataProvider<Location>();
    table = new CellTable<Location>();

    /* Create all the columns */
    
    ClickableTextCell nameCell = new ClickableTextCell();
    Column<Location, String> nameColumn = new Column<Location, String>(nameCell){
      @Override
      public String getValue(Location object) {
        return object.getName();
      }
    };
    
    TextColumn<Location> foodTypeColumn = new TextColumn<Location>(){
      @Override
      public String getValue(Location object) {
        return object.getFoodType().toString();
      }
    };
    
    TextColumn<Location> notesColumn = new TextColumn<Location>(){
      @Override
      public String getValue(Location object) {
        return object.getNotes();
      }
    };
    
    ClickableTextCell dropOffTimesCell = new ClickableTextCell();
	Column<Location, String> dropOffTimesColumn = new Column<Location, String>(dropOffTimesCell){
		@Override
		public String getValue(Location object) {
			return "Drop Off Times";
		}
	};
	dropOffTimesColumn.setCellStyleNames(Resources.INSTANCE.ops().textButtonCell());
    
    TextColumn<Location> ownerColumn = new TextColumn<Location>(){
      @Override
      public String getValue(Location object) {
        return object.getCreatorEmail();
      }
    };
    
	ClickableTextCell editCell = new ClickableTextCell();
	Column<Location, String> editColumn = new Column<Location, String>(editCell){
		@Override
		public String getValue(Location object) {
			return "Edit";
		}
	};
	editColumn.setCellStyleNames(Resources.INSTANCE.ops().textButtonCell());

    /* Add the columns to the table */
    table.addColumn(nameColumn, "Name");
    table.addColumn(foodTypeColumn, "Food Type");
    table.addColumn(notesColumn, "Notes");
    table.addColumn(ownerColumn, "Owner");
    table.addColumn(dropOffTimesColumn, "Drop Off Times");
    table.addColumn(editColumn, "Edit");

    dataProvider.addDataDisplay(table);

    ListHandler<Location> sortHandler = new ListHandler<Location>(dataProvider.getList());
    table.addColumnSortHandler(sortHandler);

    /* Set the columns as sortable and add their comparators to the sort handler */
    nameColumn.setSortable(true);
    sortHandler.setComparator(nameColumn, new Comparator<Location>(){
      @Override
      public int compare(Location o1, Location o2) {
        return o1.getName().compareTo(o2.getName());
      }
    });
    
    foodTypeColumn.setSortable(true);
    sortHandler.setComparator(foodTypeColumn, new Comparator<Location>(){
      @Override
      public int compare(Location o1, Location o2) {
        return o1.getFoodType().toString().compareTo(o2.getFoodType().toString());
      }
    });

    ownerColumn.setSortable(true);
    sortHandler.setComparator(ownerColumn, new Comparator<Location>(){
      @Override
      public int compare(Location o1, Location o2) {
        return o1.getCreatorEmail().compareTo(o2.getCreatorEmail());
      }
    });
    
    dropOffTimesColumn.setFieldUpdater(new FieldUpdater<Location, String>(){
		@Override
		public void update(int index, final Location object, String value) {
			recipientInterface.onTimeSlotViewOpen(object);
		}
	});
    
	editColumn.setFieldUpdater(new FieldUpdater<Location, String>(){
		@Override
		public void update(int index, final Location object, String value) {
			recipientInterface.onEditLocation(object);
		}
	});

    
    this.add(table);
    table.setWidth("100%");
    //this.setHeight("100%");
    table.setEmptyTableWidget(new HTML("No pickups are available to view."));
  }
  public void addLocation(Location location){
    dataProvider.getList().add(location);
  }
  public void removeLocation(Location location){
    dataProvider.getList().remove(location);
  }
  public void updateLocation()
  {
    table.redraw();
  }
}
